package jatosoft.accounting.webapp.managedbean;

import jatosoft.accounting.dao.model.Account;
import jatosoft.accounting.service.AccountService;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.component.UIInput;
import javax.faces.component.UIOutput;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import com.icesoft.faces.component.selectinputtext.SelectInputText;

public class SelectHelper extends BaseManagedBean {

	private static final long serialVersionUID = 4316538790546901684L;

	private AccountService accountService;

	private List<SelectItem> sltAccountsWithEmpty = new ArrayList<SelectItem>();

	private List<Account> allAccounts;

	private List<List<SelectItem>> selectCandidateAccounts;

	private String[] accountNames = new String[11];

	public String[] getAccountNames() {
		return accountNames;
	}

	public void setAccountNames(String[] accountNames) {
		this.accountNames = accountNames;
	}

	public List<List<SelectItem>> getSelectCandidateAccounts() {
		return selectCandidateAccounts;
	}

	public void setSelectCandidateAccounts(
			List<List<SelectItem>> selectCandidateAccounts) {
		this.selectCandidateAccounts = selectCandidateAccounts;
	}

	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}

	public List<SelectItem> getSltAccountsWithEmpty() {
		return sltAccountsWithEmpty;
	}

	public void setSltAccountsWithEmpty(List<SelectItem> sltAccountsWithEmpty) {
		this.sltAccountsWithEmpty = sltAccountsWithEmpty;
	}

	@PostConstruct
	public void initPage() {

		List<Account> accounts = accountService.listAccount(null);
		sltAccountsWithEmpty.add(new SelectItem());
		for (Account account : accounts) {
			SelectItem slt = new SelectItem();
			slt.setLabel(account.getAccountId() + "-"
					+ account.getAccountName());
			slt.setValue(account.getAccountId());
			sltAccountsWithEmpty.add(slt);
		}

		allAccounts = accountService.listAccount(null);
		selectCandidateAccounts = new ArrayList<List<SelectItem>>();
		for (int i = 0; i < 11; i++) {
			List<SelectItem> slis = new ArrayList<SelectItem>();
			for (Account a : allAccounts) {
				SelectItem sli = new SelectItem(a, a.getAccountId());
				slis.add(sli);
			}
			selectCandidateAccounts.add(slis);

		}

	}

	public void selectInputValueChanged(ValueChangeEvent vEvent) {
		if (!(vEvent.getOldValue() == null && "".equals(vEvent.getNewValue()))) {
			// ignore not changed text
			
			SelectInputText selectAccount = (SelectInputText) vEvent
					.getComponent();
			Integer seq = (Integer) ((UIOutput) selectAccount.getParent()
					.findComponent("hidId")).getValue();
			if(logger.isDebugEnabled()){
				logger.debug("Current voucher item seq: "+ seq);
			}
			if (seq != null) {
				filterSelectCandidate(seq - 1, (String) vEvent.getNewValue());
				selectAccount.updateModel(getFacesContext());
//				if (logger.isDebugEnabled()) {
//					logger.debug("accountNames.size(): " + accountNames.size());
//				}
				if (allAccounts.contains(new Account((String) vEvent
						.getNewValue()))) {
					accountNames[seq-1]=allAccounts.get(
							allAccounts.lastIndexOf(new Account((String) vEvent
									.getNewValue()))).getAccountName();
					
					
					
					
					
					
					
					
					
					/*
					
					if (accountNames.size() < seq) {
						accountNames.add( allAccounts.get(
								allAccounts.lastIndexOf(new Account((String) vEvent
										.getNewValue()))).getAccountName());
					}else{
						
					}

				} else {
					accountNames.add(seq - 1, "");*/
				}

			}
		}
	}

	private void filterSelectCandidate(int ii, String value) {
		selectCandidateAccounts.get(ii).clear();
		for (Account a : allAccounts) {
			SelectItem sli = new SelectItem(a, a.getAccountId());
			selectCandidateAccounts.get(ii).add(sli);
		}
		if (value != null && !"".equals(value)) {
			for (int i = 0; i < selectCandidateAccounts.get(ii).size();) {
				Account account = (Account) selectCandidateAccounts.get(ii)
						.get(i).getValue();
				if (!account.getAccountId().startsWith(value)
						&& !account.getAccountName().startsWith(value)) {
					selectCandidateAccounts.get(ii).remove(i);
				} else {
					i++;
				}
			}
		}
	}

	public void updateAccountName(int seq, Account account) {
		
	}

	public void clearAccountNameList() {
		sltAccountsWithEmpty = new ArrayList<SelectItem>();
	}
}
